/* Raw IR commander
 
 This sketch/program uses the Arduno and a PNA4602 to 
 decode IR received.  It then attempts to match it to a previously
 recorded IR signal
 
 Code is public domain, check out www.ladyada.net and adafruit.com
 for more tutorials! 
 */

// We need to use the 'raw' pin reading methods
// because timing is very important here and the digitalRead()
// procedure is slower!
//uint8_t IRpin = 2;
// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#define IRpin_PIN      PIND
#define IRpin          3

// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000

// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20 

// What percent we will allow in variation to match the same code
#define FUZZINESS 20

// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2];  // pair is high and low pulse 
uint8_t currentpulse = 0; // index for pulses we're storing


int ApplePlaySignal[] = {
// ON, OFF (in 10's of microseconds)
	240, 56,
	64, 56,
	62, 58,
	62, 58,
	66, 52,
	62, 58,
	62, 58,
	62, 56,
	122, 58,
	66, 54,
	62, 56,
	62, 58,
	62, 2684,
	238, 58,
	66, 54,
	62, 56,
	62, 58,
	62, 58,
	62, 56,
	68, 52,
	62, 58,
	120, 58,
	68, 52,
	62, 56,
	64, 56,
	62, 2684,
	244, 52,
	62, 58,
	62, 56,
	62, 58,
	62, 58,
	62, 58,
	62, 56,
	62, 58,
	120, 60,
	60, 58,
	62, 56,
	68, 52,
	64, 2682,
	238, 58,
	62, 58,
	62, 56,
	64, 56,
	64, 56,
	62, 58,
	62, 58,
	62, 56,
	122, 56,
	62, 58,
	62, 58,
	62, 58,
	66, 0};

int AppleForwardSignal[] = {
// ON, OFF (in 10's of microseconds)
	244, 52,
	122, 56,
	62, 58,
	62, 58,
	62, 56,
	64, 56,
	62, 58,
	62, 58,
	126, 52,
	62, 56,
	62, 58,
	62, 58,
	62, 2624,
	240, 56,
	122, 56,
	62, 58,
	62, 58,
	62, 56,
	64, 56,
	64, 56,
	68, 52,
	120, 58,
	62, 58,
	62, 56,
	68, 52,
	68, 2620,
	238, 58,
	126, 52,
	62, 56,
	62, 58,
	62, 58,
	62, 56,
	68, 52,
	62, 58,
	126, 52,
	62, 58,
	68, 52,
	62, 56,
	64, 0};

int AppleRewindSignal[] = {
// ON, OFF (in 10's of microseconds)
	238, 56,
	64, 56,
	122, 56,
	62, 58,
	62, 58,
	62, 56,
	64, 58,
	62, 56,
	122, 56,
	62, 58,
	62, 58,
	62, 56,
	62, 2626,
	238, 60,
	60, 58,
	120, 58,
	62, 56,
	62, 58,
	62, 58,
	62, 56,
	64, 56,
	122, 56,
	64, 56,
	62, 58,
	62, 58,
	62, 2624,
	240, 56,
	62, 58,
	120, 60,
	60, 58,
	62, 56,
	62, 58,
	62, 58,
	62, 56,
	122, 56,
	64, 56,
	62, 58,
	62, 58,
	62, 2624,
	240, 56,
	62, 58,
	122, 56,
	62, 58,
	62, 58,
	62, 56,
	62, 58,
	62, 58,
	120, 58,
	62, 56,
	64, 56,
	62, 58,
	62, 2624,
	240, 56,
	64, 56,
	122, 56,
	62, 58,
	62, 58,
	62, 56,
	64, 56,
	64, 56,
	122, 56,
	62, 58,
	62, 58,
	62, 56,
	62, 0};

void setup(void) {
  Serial.begin(9600);
  Serial.println("Ready to decode IR!");
}

void loop(void) {
  int numberpulses;
  
  numberpulses = listenForIR();
  
  Serial.print("Heard ");
  Serial.print(numberpulses);
  Serial.println("-pulse long IR signal");
  if (IRcompare(numberpulses, ApplePlaySignal)) {
    Serial.println("PLAY");
  }
    if (IRcompare(numberpulses, AppleRewindSignal)) {
    Serial.println("REWIND");
  }
    if (IRcompare(numberpulses, AppleForwardSignal)) {
    Serial.println("FORWARD");
  }
}

boolean IRcompare(int numpulses, int Signal[]) {
  
  for (int i=0; i< numpulses-1; i++) {
    int oncode = pulses[i][1] * RESOLUTION / 10;
    int offcode = pulses[i+1][0] * RESOLUTION / 10;
    
    /*
    Serial.print(oncode); // the ON signal we heard
    Serial.print(" - ");
    Serial.print(Signal[i*2 + 0]); // the ON signal we want 
    */
    
    // check to make sure the error is less than FUZZINESS percent
    if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
      //Serial.print(" (ok)");
    } else {
      //Serial.print(" (x)");
      // we didn't match perfectly, return a false match
      return false;
    }
    
    /*
    Serial.print("  \t"); // tab
    Serial.print(offcode); // the OFF signal we heard
    Serial.print(" - ");
    Serial.print(Signal[i*2 + 1]); // the OFF signal we want 
    */
    
    if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
      //Serial.print(" (ok)");
    } else {
      //Serial.print(" (x)");
      // we didn't match perfectly, return a false match
      return false;
    }
    
    //Serial.println();
  }
  // Everything matched!
  return true;
}

int listenForIR(void) {
  currentpulse = 0;
  
  while (1) {
    uint16_t highpulse, lowpulse;  // temporary storage timing
    highpulse = lowpulse = 0; // start out with no pulse length
  
//  while (digitalRead(IRpin)) { // this is too slow!
    while (IRpin_PIN & (1 << IRpin)) {
       // pin is still HIGH

       // count off another few microseconds
       highpulse++;
       delayMicroseconds(RESOLUTION);

       // If the pulse is too long, we 'timed out' - either nothing
       // was received or the code is finished, so print what
       // we've grabbed so far, and then reset
       if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
         return currentpulse;
       }
    }
    // we didn't time out so lets stash the reading
    pulses[currentpulse][0] = highpulse;
  
    // same as above
    while (! (IRpin_PIN & _BV(IRpin))) {
       // pin is still LOW
       lowpulse++;
       delayMicroseconds(RESOLUTION);
       if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
         return currentpulse;
       }
    }
    pulses[currentpulse][1] = lowpulse;

    // we read one high-low pulse successfully, continue!
    currentpulse++;
  }
}

/*
void printpulses(void) {
  Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
  for (uint8_t i = 0; i < currentpulse; i++) {
    Serial.print(pulses[i][0] * RESOLUTION, DEC);
    Serial.print(" usec, ");
    Serial.print(pulses[i][1] * RESOLUTION, DEC);
    Serial.println(" usec");
  }
  
  // print it in a 'array' format
  Serial.println("int IRsignal[] = {");
  Serial.println("// ON, OFF (in 10's of microseconds)");
  for (uint8_t i = 0; i < currentpulse-1; i++) {
    Serial.print("\t"); // tab
    Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
    Serial.print(", ");
    Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
    Serial.println(",");
  }
  Serial.print("\t"); // tab
  Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
  Serial.print(", 0};");
}
*/
